package com.fuxi.cloud.desreport.service.a;

import com.alibaba.druid.util.StringUtils;
import com.fuxi.cloud.desreport.dao.JimuReportShareDao;
import com.fuxi.cloud.desreport.entity.JimuReportShare;
import com.fuxi.cloud.desreport.service.IJimuReportShareService;
import org.jeecgframework.minidao.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
@Service("jimuReportShareServiceImpl")
public class e implements IJimuReportShareService {
    private static final Logger a = LoggerFactory.getLogger(e.class);
    @Autowired
    private JimuReportShareDao jimuReportShareDao;

    public e() {
    }

    @Override
    public JimuReportShare selectJurisdiction(String reportId) {
        return this.jimuReportShareDao.selectJurisdiction(reportId);
    }

    @Override
    public String getExtractionCode() {
        String var1 = "0123456789abcdefghijklmnopqrstuvwxyz";
        StringBuilder var2 = new StringBuilder(4);

        for(int var3 = 0; var3 < 4; ++var3) {
            char var4 = var1.charAt((new Random()).nextInt(var1.length()));
            var2.append(var4);
        }

        return var2.toString();
    }

    @Override
    public JimuReportShare compareToDate(JimuReportShare jurisdiction) {
        Date var2 = jurisdiction.getLastUpdateTime();
        String var3 = jurisdiction.getTermOfValidity();
        Calendar var4 = Calendar.getInstance();
        var4.setTime(var2);
        if ("2".equals(var3)) {
            var4.add(5, 7);
        } else {
            if (!"3".equals(var3)) {
                return jurisdiction;
            }

            var4.add(5, 1);
        }

        Date var5 = var4.getTime();
        Date var6 = new Date();
        boolean var7 = var6.before(var5);
        if (!var7) {
            jurisdiction.setStatus("1");
            this.jimuReportShareDao.update(jurisdiction);
            return jurisdiction;
        } else {
            return jurisdiction;
        }
    }

    @Override
    public String passwordVerification(String reportId, String lock) {
        JimuReportShare var3 = this.jimuReportShareDao.selectJurisdiction(reportId);
        if (null == var3) {
            return "3";
        } else {
            var3 = this.compareToDate(var3);
            if ("1".equals(var3.getStatus())) {
                return "1";
            } else if (StringUtils.isEmpty(lock)) {
                return "2";
            } else {
                String var4 = var3.getPreviewLock();
                return var4.equals(lock) ? "0" : "2";
            }
        }
    }

    @Override
    public JimuReportShare addAndEdit(JimuReportShare jurisdiction) {
        String var2 = jurisdiction.getReportId();
        String var3 = this.getExtractionCode();
        JimuReportShare var4 = this.jimuReportShareDao.selectJurisdiction(var2);
        jurisdiction.setLastUpdateTime(new Date());
        jurisdiction.setPreviewUrl("/jmreport/shareView/" + jurisdiction.getReportId());
        jurisdiction.setPreviewLock(var3);
        if (null == var4) {
            String var5 = String.valueOf(SnowflakeIdWorker.generateId());
            jurisdiction.setId(var5);
            this.jimuReportShareDao.insert(jurisdiction);
        } else {
            jurisdiction.setId(var4.getId());
            this.jimuReportShareDao.update(jurisdiction);
        }

        return jurisdiction;
    }
}

